package cn.powerone.scm.service.impl;

import java.util.List;

import cn.powerone.common.exception.ServiceException;
import cn.powerone.common.utils.DateUtils;
import cn.powerone.common.utils.StringUtils;
import cn.powerone.scm.domain.ScmSupplier;
import cn.powerone.scm.mapper.ScmSupplierMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.powerone.scm.mapper.ScmSupplierContactsMapper;
import cn.powerone.scm.domain.ScmSupplierContacts;
import cn.powerone.scm.service.IScmSupplierContactsService;

/**
 * 供应商联系人Service业务层处理
 * 
 * @author PowerOne
 * @date 2025-06-23
 */
@Slf4j
@Service
public class ScmSupplierContactsServiceImpl implements IScmSupplierContactsService 
{
    @Autowired
    private ScmSupplierContactsMapper scmSupplierContactsMapper;

    @Autowired
    private ScmSupplierMapper scmSupplierMapper;

    /**
     * 查询供应商联系人
     * 
     * @param id 供应商联系人主键
     * @return 供应商联系人
     */
    @Override
    public ScmSupplierContacts selectScmSupplierContactsById(Long id)
    {
        return scmSupplierContactsMapper.selectScmSupplierContactsById(id);
    }

    /**
     * 查询供应商联系人
     *
     * @param supplierId 供应商ID
     * @return 供应商联系人
     */
    @Override
    public ScmSupplierContacts selectScmSupplierContactsBySupplierId(Long supplierId)
    {
        return scmSupplierContactsMapper.selectScmSupplierContactsBySupplierId(supplierId);
    }

    /**
     * 查询供应商联系人列表
     * 
     * @param scmSupplierContacts 供应商联系人
     * @return 供应商联系人
     */
    @Override
    public List<ScmSupplierContacts> selectScmSupplierContactsList(ScmSupplierContacts scmSupplierContacts)
    {
        return scmSupplierContactsMapper.selectScmSupplierContactsList(scmSupplierContacts);
    }

    /**
     * 新增供应商联系人
     * 
     * @param scmSupplierContacts 供应商联系人
     * @return 结果
     */
    @Override
    public int insertScmSupplierContacts(ScmSupplierContacts scmSupplierContacts)
    {
        scmSupplierContacts.setCreateTime(DateUtils.getNowDate());
        return scmSupplierContactsMapper.insertScmSupplierContacts(scmSupplierContacts);
    }

    /**
     * 修改供应商联系人
     * 
     * @param scmSupplierContacts 供应商联系人
     * @return 结果
     */
    @Override
    public int updateScmSupplierContacts(ScmSupplierContacts scmSupplierContacts)
    {
        scmSupplierContacts.setUpdateTime(DateUtils.getNowDate());
        return scmSupplierContactsMapper.updateScmSupplierContacts(scmSupplierContacts);
    }

    /**
     * 批量删除供应商联系人
     * 
     * @param ids 需要删除的供应商联系人主键
     * @return 结果
     */
    @Override
    public int deleteScmSupplierContactsByIds(Long[] ids)
    {
        return scmSupplierContactsMapper.deleteScmSupplierContactsByIds(ids);
    }

    /**
     * 删除供应商联系人信息
     * 
     * @param id 供应商联系人主键
     * @return 结果
     */
    @Override
    public int deleteScmSupplierContactsById(Long id)
    {
        return scmSupplierContactsMapper.deleteScmSupplierContactsById(id);
    }

    /** 导入数据
     * @param scmSupplierContacts 导入询价Batch列表
     */
    @Override
    public String importData(List<ScmSupplierContacts> scmSupplierContacts, boolean updateSupport, String operName)
    {
        if (StringUtils.isNull(scmSupplierContacts) || scmSupplierContacts.size() == 0)
        {
            throw new RuntimeException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        ScmSupplier customerInq = new ScmSupplier();
        ScmSupplierContacts scmSupplierContactsInq = new ScmSupplierContacts();
        List<ScmSupplier> customerOutList;
        long numberOfResult = 0;
        for (ScmSupplierContacts item : scmSupplierContacts)
        {
            if(item.getSupplierNo() != null && !item.getSupplierNo().isEmpty()){
                customerInq.setSupplierNo(item.getSupplierNo());
                customerOutList = scmSupplierMapper.selectScmSupplierList(customerInq);
                if(customerOutList.isEmpty()){
                    continue;
                }
                item.setSupplierId(customerOutList.get(0).getId());
            }
            if (!updateSupport){
                scmSupplierContactsInq.setSupplierId(item.getSupplierId());
                scmSupplierContactsInq.setName(item.getName());
                scmSupplierContactsInq.setMobile(item.getMobile());
                numberOfResult = scmSupplierContactsMapper.selectScmSupplierContactsList(scmSupplierContactsInq).size();
                if (numberOfResult > 0 ){
                    continue;
                }
            }
            try
            {
                scmSupplierContactsMapper.insertScmSupplierContacts( item);
                successNum++;
            } catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "姓名: " + item.getName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0){
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条联系人数据格式不正确。");
            throw new ServiceException(failureMsg.toString());
        }else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条。");
        }
        return successMsg.toString();

    }
}
